package au.com.iglootech.TaxTimeToolTip;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import au.com.iglootech.TaxTimeToolTip.Entity.User;

import com.vaadin.Application;
import com.vaadin.service.ApplicationContext.TransactionListener;

public class UserSessionData implements TransactionListener, Serializable {
//    private ResourceBundle bundle;
//    private Locale locale;   // Current locale
    private User userData; // Trivial data model for the user
    
    private Application app; // For distinguishing between apps

    private static ThreadLocal<UserSessionData> instance =
        new ThreadLocal<UserSessionData>();
    
    public UserSessionData(Application app) {
        this.app = app;

        // It's usable from now on in the current request
        instance.set(this);
    }
    
    public void transactionEnd(Application application)
    {
        if (this.app == application)
            instance.set(null);
    }
    
    public void transactionStart(Application application)
    {
        if (this.app == application)
            instance.set(this);
    }

    @Override
    public void transactionStart(Application application,
                                 Object transactionData) {
        // Set this data instance of this application
        // as the one active in the current thread. 
        if (this.app == application)
            instance.set(this);
    }

    @Override
    public void transactionEnd(Application application,
                               Object transactionData) {
        // Clear the reference to avoid potential problems
        if (this.app == application)
            instance.set(null);
    }

//    public static void initLocale(Locale locale,
//                                  String bundleName) {
//        instance.get().locale = locale;
//        instance.get().bundle =
//            ResourceBundle.getBundle(bundleName, locale);
//    }
//    
//    public static Locale getLocale() {
//        return instance.get().locale;
//    }
//
//    public static String getMessage(String msgId) {
//        return instance.get().bundle.getString(msgId);
//    }

    public static User getUserData() {
        return instance.get().userData;
    }

    public static void setUserData(User userData) {
        instance.get().userData = userData;
    }
}